package com.exd.kafka.task;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class FileConversion {
	
	public static void main(String[] args){
		// 写出文件
		FileWriter fileWriter = null;
		BufferedWriter bufferedWriter = null;
		// 读入文件
		FileReader fileReader = null;
		BufferedReader bufferedReader = null;
		try {
			fileWriter = new FileWriter("E:/alerts.txt");
			bufferedWriter = new BufferedWriter(fileWriter);
			fileReader = new FileReader("E:/ck.csv");
			bufferedReader = new BufferedReader(fileReader);
			String lineDataValue = null;
			int readDataNumber = 3;
			int readCountNumber = 0;
			while ((lineDataValue = bufferedReader.readLine()) != null) {
				readCountNumber = readCountNumber+1;
				if(readCountNumber > readDataNumber)return;
				String[] dataValueSplit = lineDataValue.split(",");
				String vin =dataValueSplit[0].replace("\"", "");
				String collectionTime =dataValueSplit[3].replace("\"", "");
				String data =dataValueSplit[10].replace("\"", "");
				Map<String,Object> dataValueMap = new HashMap<>();
				dataValueMap.put("vin", vin);
				dataValueMap.put("collectionTime",stringToDate(collectionTime).getTime());
				dataValueMap.put("data",data);
				bufferedWriter.write(JsonTool.ObjectToJson(dataValueMap));
				bufferedWriter.newLine();
				bufferedWriter.flush();	
		    }
		} catch (Exception exception) {
			exception.printStackTrace();
		}finally {
			try {
				if(bufferedReader!=null)bufferedReader.close();
				if(fileReader!=null)fileReader.close();
				if(bufferedWriter != null)bufferedWriter.close();
				if(fileWriter != null)fileWriter.close();	
			} catch (Exception exception) {
				exception.printStackTrace();
			}
		}
	}
	
	/**
	 * 描述: 字符串转日期
	 * 创建人: 赵兴炎
	 * 日期: 2022年9月13日
	 */
	public static Date stringToDate(String dateTime) {
		Date dateTimeValue = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		try {
			dateTimeValue = simpleDateFormat.parse(dateTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateTimeValue;
	}

}
